package com.wenbo.wenbo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wenbo.wenbo.dto.StoryDto;
import com.wenbo.wenbo.dto.TxDataDto;
import com.wenbo.wenbo.entity.Story;
import com.wenbo.wenbo.mapper.StoryMapper;
import com.wenbo.wenbo.service.StoryService;
import com.wenbo.wenbo.utils.HttpRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ywb
 * @date 2021年12月08日 10:03
 */
@Service
public class StoryServiceImpl extends ServiceImpl<StoryMapper, Story> implements StoryService {

    private static final Logger log = LoggerFactory.getLogger(StoryServiceImpl.class);
    @Value("${story.api}")
    private String api;
    @Value("${story.key}")
    private String key;


    @Override
    public List<StoryDto> getStory(String name, String type) {
        LambdaQueryWrapper<Story> wrapper1 = Wrappers.lambdaQuery(Story.class);
        wrapper1.eq(Story::getType, type);
        wrapper1.orderByDesc(Story::getPage);
        wrapper1.last("limit 1");
        Story s = getOne(wrapper1);
        Integer max = s.getPage();
        Integer min = 1;
        int index = (int) (Math.random() * (max - min)) + min;
        LambdaQueryWrapper<Story> wrapper = Wrappers.lambdaQuery(Story.class);
        wrapper.eq(Story::getType, type);
        wrapper.eq(Story::getPage, index);
        wrapper.like(StringUtils.isNotBlank(name), Story::getTitle, name);
        return baseMapper.selectList(wrapper).stream().map(x -> new StoryDto(x.getId(), x.getTitle())).collect(Collectors.toList());
    }

    @Override
    public StoryDto getStoryInfo(String id) {
        StoryDto storyDto = new StoryDto();
        Story story = baseMapper.selectById(id);
        BeanUtil.copyProperties(story, storyDto);
        return storyDto;
    }

    @Override
    public void getStoryList() throws InterruptedException {
        for (int i = 223; i < 230; i++) {
            //getStory(3,i);223
            //getStory(4,i);
            //Thread.sleep(1000);
        }
    }


    private void getStory(Integer type, Integer page) {
        StringBuffer sb = new StringBuffer();
        sb.append("key=").append(key).append("&num=5").append("&type=").append(type).append("&page=").append(page);
        String res = HttpRequest.sendGet(api, sb.toString());
        TxDataDto txDataDto = JSONUtil.toBean(res, TxDataDto.class);
        if (txDataDto.getCode().equals("200")) {
            List<StoryDto> list = txDataDto.getNewslist();
            for (StoryDto storyDto : list) {
                saveStory(storyDto, type, page);
            }

        }
    }

    private void saveStory(StoryDto storyDto, Integer type, Integer page) {
        Story s = new Story();
        s.setContent(storyDto.getContent());
        s.setTitle(storyDto.getTitle());
        s.setType(type);
        s.setPage(page);
        s.insert();
    }
}
